package com.varton.demo.dao;

import com.varton.demo.model.Company;
import com.varton.framework.common.BaseDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.core.common.utils.StringUtils;
import javax.core.jdbc.DynamicDataSource;
import javax.sql.DataSource;

@Repository
public class CompanyDao extends BaseDaoSupport<Company,String> {
    private DynamicDataSource dataSource;

    protected String getPKColumn() {
        return "id";
    }

    @Resource(name="dynamicDataSource")
    protected void setDataSource(DataSource dataSource) {
        this.dataSource = (DynamicDataSource)dataSource;
        this.setDataSourceReadOnly(dataSource);
        this.setDataSourceWrite(dataSource);

    }

    public boolean insert(Company entity) throws Exception{
//        if(entity.getAge() >= 30){
//            this.dataSource.getDataSourceEntry().set("db_two");
//        }else{
//            this.dataSource.getDataSourceEntry().set("db_one");
//        }
        String id = super.insertAndReturnId(entity);
        return !StringUtils.isEmpty(id);
    }

}
